<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\ItemCard as ItemCardModel;
use app\common\model\GoodsList;

class ItemCard extends Base{
	/**
	 * 优惠券列表
	 */
	public function index(Request $request){
		$keyword=trim(input('get.keyword'));
		if(!empty($keyword)){
			$whe[]=['title','like','%'.$keyword.'%'];
		
		}
        $whe[]= ['status','<',3];
		$field = "*";
		$order = "addtime desc";
		$data = ItemCardModel::getList($whe,$field,$order,$this->page);
		View::assign('list',$data['list']);
		View::assign('count',$data['count']);
		View::assign('page',$data['page']);
		return View::fetch("ItemCard/index");
	
	}

	/**
	 * 添加商品
	 */
	public function add(Request $request){
		if(!$request->isPost()){
			
			return View::fetch("ItemCard/add");
		}else{
			$data = input('post.');

            $data['status'] = 1;
            $data['addtime'] = time();
			$dd=ItemCardModel::insertGetId($data);
			
			if($dd){
				return $this->renderSuccess("添加成功");
			}else{
				return $this->renderError("网络繁忙，请稍后");
			}
		}
	}

	/**
	 * 编辑商品
	 */
	 public function edit(Request $request){
		if(!$request->isPost()){
			$id = $request->param('id');
			$goods = ItemCardModel::getInfo(['id'=>$id],'*');
			View::assign('data',$goods);
			return View::fetch("ItemCard/edit");
		}else{
			$data = input('post.');
            ItemCardModel::startTrans();
            $id = $data['id'];
            unset($data['id']);
            $data['updatetime'] = time();
			$dd=ItemCardModel::where(['id'=>$id])->update($data);
			
			if($dd){
                ItemCardModel::commit();
				return $this->renderSuccess("编辑成功");
			}else{
                ItemCardModel::rollback();
				return $this->renderError("网络繁忙，请稍后");
			}

		}
	}

	//上、下架商品
	public function cao(Request $request){
		$id=$request->post('id/d');
		$status=$request->post('type/d');
		if(!in_array($status,array(1,2,3))){
			return $this->renderError("数据错误");
		}
		$data=ItemCardModel::where(['id'=>$id])->field('id')->find();
		if(!$data){
			return $this->renderError("数据不存在");
		}
		$result = ItemCardModel::where(['id'=>$id])->update(["status"=>$status]);
		if($result){
			return $this->renderSuccess("操作成功");
		}else{
			return $this->renderError("网络繁忙，请稍后");
		}
	}

	
}
